package com.hlabexamples.fabrevealmenu;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;

/**
 * MainAbility
 *
 * @since 2021-05-14
 */
public class MainAbility extends FractionAbility {
    private static final int NUM_1 = 1;
    private static final int NUM_2 = 2;
    private static final int NUM_3 = 3;
    private static final String TITLE_COLOR = "#1976d2";
    private final Image[] images = new Image[NUM_3];
    private final int[] imageIds = new int[]{
        ResourceTable.Id_img_xml,ResourceTable.Id_img_code,ResourceTable.Id_img_custom};
    private final Image[] imageSelecteds = new Image[NUM_3];
    private final int[] imageSelectedIds = new int[]{
        ResourceTable.Id_img_xml_selected,
        ResourceTable.Id_img_code_selected,ResourceTable.Id_img_custom_select};
    private final Text[] texts = new Text[NUM_3];
    private final Text[] toastTexts = new Text[NUM_3];
    private final int[] textIds = new int[]{ResourceTable.Id_t_xml,ResourceTable.Id_t_code,ResourceTable.Id_t_custom};
    private final int[] toastIds = new int[]{ResourceTable.Id_xml_toast,
        ResourceTable.Id_code_toast,ResourceTable.Id_custom_toast};
    private final Fraction[] fractions = new Fraction[]{new DemoXmlFraction(),new DemoCodeFraction()};
    private int mPosition = 0;
    private int mOldPosition = 0;
    private int mToastPosition = 0;
    private DirectionalLayout dlToast;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor(TITLE_COLOR));
        super.setUIContent(ResourceTable.Layout_ability_main);
        dlToast = (DirectionalLayout)findComponentById(ResourceTable.Id_dl_toast);
        for (int i = 0;i < NUM_3;i++) {
            images[i] = (Image)findComponentById(imageIds[i]);
            imageSelecteds[i] = (Image)findComponentById(imageSelectedIds[i]);
            texts[i] = (Text)findComponentById(textIds[i]);
            toastTexts[i] = (Text)findComponentById(toastIds[i]);
            toastTexts[i].setLongClickable(true);
        }
        DirectionalLayout dlXml = (DirectionalLayout)findComponentById(ResourceTable.Id_dl_xml);
        DirectionalLayout dlCode = (DirectionalLayout)findComponentById(ResourceTable.Id_dl_code);
        DirectionalLayout dlCustom = (DirectionalLayout)findComponentById(ResourceTable.Id_dl_custom);
        dlXml.setClickedListener(component -> btnSelectedByPosition(0));
        dlCode.setClickedListener(component -> btnSelectedByPosition(NUM_1));
        dlCustom.setClickedListener(component -> btnSelectedByPosition(NUM_2));
        dlXml.setLongClickedListener(component -> btnLongClickByPosition(0));
        dlCode.setLongClickedListener(component -> btnLongClickByPosition(NUM_1));
        dlCustom.setLongClickedListener(component -> btnLongClickByPosition(NUM_2));
        getFractionManager()
                .startFractionScheduler()
                .replace(ResourceTable.Id_container,fractions[mPosition])
                .submit();
    }

    private void btnSelectedByPosition(int position) {
        if (mPosition != position) {
            imageSelecteds[mPosition].setVisibility(Component.HIDE);
            images[mPosition].setVisibility(Component.VISIBLE);
            texts[mPosition].setTextColor(Color.BLACK);
            imageSelecteds[position].setVisibility(Component.VISIBLE);
            images[position].setVisibility(Component.HIDE);
            texts[position].setTextColor(new Color(Color.getIntColor(TITLE_COLOR)));
            if (position != Constants.INT_2) {
                getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_container,fractions[position])
                        .submit();
            } else {
                mOldPosition = mPosition;
                nextPage(ScrollingAbility.class.getName());
            }
            mPosition = position;
        }
    }

    private void btnLongClickByPosition(int position) {
        dlToast.setVisibility(Component.VISIBLE);
        for (int i = 0;i < NUM_3;i++) {
            toastTexts[i].setLongClickable(false);
        }
        toastTexts[mToastPosition].setVisibility(Component.INVISIBLE);
        toastTexts[position].setVisibility(Component.VISIBLE);
        mToastPosition = position;
        getMainTaskDispatcher().delayDispatch(() -> {
            dlToast.setVisibility(Component.HIDE);
            for (int i = 0;i < NUM_3;i++) {
                toastTexts[i].setLongClickable(true);
            }
            toastTexts[mToastPosition].setVisibility(Component.INVISIBLE);
        }, Constants.INT_3000);
    }

    @Override
    protected void onActive() {
        super.onActive();
        btnSelectedByPosition(mOldPosition);
    }

    private void nextPage(String abilityName) {
        Intent intent = new Intent();
        Operation build = new Intent.OperationBuilder()
                .withBundleName(getBundleName())
                .withAbilityName(abilityName)
                .build();
        intent.setOperation(build);
        startAbility(intent);
    }
}
